<?php
namespace Cli\Controller;

class IndexController extends CliController{

    public function index(){
		echo 'test';
    }

    /*
     * 未支付订单过期
     */
    public function expire()
    {
        $orderExpireTime = intval(getConfig('order_expire_time'));
        $orderExpireTime = empty($orderExpireTime) ? 15 : $orderExpireTime;
        $time = time();
        $diffTime = $time - ($orderExpireTime * 60);
        $where = array( 'order_state' => 1, 'create_time' => array( 'lt', $diffTime ) );
        $result = M('Order')->where($where)->save(array( 'order_state' => 0, 'state_info' => '支付过期系统取消', 'update_time' => time() ));
        trace('expire change row = ' . $result, '', 'DEBUG', true);
        exit('expire change row = ' . $result . PHP_EOL);
    }
    
    /*
     * 订单自动完成
     */
    public function finish()
    {
        set_time_limit(0);
        $time = time();
        $orderFinishTime = intval(getConfig('order_finish_time'));
        $orderFinishTime = empty($orderFinishTime) ? 7 : $orderFinishTime;
        $diffTime = $time - ( $orderFinishTime * 86400 );
        $where = array( 'order_state' => 3, 'create_time' => array( 'lt' , $diffTime ) );
        $result = M('Order')->where($where)->select();
        if (empty($result)){
            trace('finish change row = 0', '', 'DEBUG', true);
            exit('finish change row = 0' . PHP_EOL);
        }
        $i = 0;
        foreach ($result as $key => $val) {
            $bool = finishOrder($val);
            if ($bool) {
                $i ++;
            }
        }
        trace('finish change row = ' . $i, '', 'DEBUG', true);
        exit('finish change row = ' . $i . PHP_EOL);
    }

    /*
     * 返现及营销
     */
    public function amway()
    {
        set_time_limit(0);
        $time = time();
        $amwayFinishTime = intval(getConfig('amway_finish_time'));
        $amwayFinishTime = empty($amwayFinishTime) ? 7 : $amwayFinishTime;
        $diffTime = $time - ($amwayFinishTime * 86400);
        $where = array( 'status' => 0, 'create_time' => array( 'lt', $diffTime ) );
        $result = M('MemberAccount')->where($where)->select();
        if (empty($result)) {
            trace('amway change row = 0', '', 'DEBUG', true);
            exit('amway change row = 0' . PHP_EOL);
        }
        $model = D();
        $i = 0;
        foreach ($result as $key => $val) {
            try {
                $model->startTrans();
                if ($val['account_type'] == 3) {
                    D('MemberCore')->setMemberMoney($val['member_id'], $val['member_money'], 'inc');
                } elseif ($val['account_type'] == 4) {
                    $amway = D('MemberAmway')->getAmway($val['member_id']);
                    if (! empty($amway['member_id'])) {
                        D('MemberCore')->setMemberMoney($amway['member_id'], $val['member_money'], 'inc');
                    }
                }
                M('MemberAccount')->where('account_id = ' . $val['account_id'])->save(array( 'status' => 1, 'update_time' => time() ));
                $i ++;
                $model->commit();
            } catch (\Exception $e) {
                $model->rollback();
            }
        }
        trace('amway change row = ' . $i, '', 'DEBUG', true);
        exit('amway change row = ' . $i . PHP_EOL);
    }
    
}

